package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private DailyContentMapper contentMapper;


    @Override
    public void save(Daily daily) {
        // 设置默认值
        daily.setCreateTime(new Date());
        dailyMapper.insert(daily);
        // 获取自动生成的主键
        DailyContent dailyContent = daily.getContent();
        dailyContent.setId(daily.getId());
        contentMapper.insert(dailyContent);
    }

    @Override
    public PageInfo query(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Daily> list = dailyMapper.queryForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void delete(Long id) {
        dailyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Daily> selectAll() {
        return dailyMapper.selectAll();
    }

    @Override
    public void changeStatus(Daily daily) {
        if (daily.getStatus() == Daily.STATUS_RELEASE) {
            //设置发布时间
            daily.setReleaseTime(new Date());
        }
        dailyMapper.changeStatus(daily);
    }

    @Override
    public DailyContent getDailyContentById(Long dailyId) {
        return contentMapper.selectDailyContentById(dailyId);
    }

    @Override
    public Integer getDailyStatus(Long userId, Long dailyId) {
        return dailyMapper.selectDailyStatus(userId,dailyId);
    }




    @Override
    public void update(Daily daily) {
        dailyMapper.updateByPrimaryKey(daily);

        // 更新游记内容
        DailyContent content = daily.getContent();
        content.setId(daily.getId());
        contentMapper.updateByPrimaryKey(content);
    }

    @Override
    public Daily get(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }

}
